<?php
namespace App\Http\Controllers;
use App\AdminUser;
use App\Notice;
use App\noticeType;
use App\Customer;
use App\Product;
use Psy\Util\Json;
use Illuminate\Support\Facades\DB;

class noticeController extends Controller{
    /**
     * 展示通讯录
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    
    public function index()
    {
       $notices= Notice::all();
        return view('admin.notice.index',compact('notices'));
    }
    
    /**
     * 添加界面
     */
    public function create(){
        return view('admin.notice.create');
    }
    /**
     * 添加操作逻辑简单实现
     */
    public function store(){

        $this->validate(request(),[
            'title'=>'required|string',
            'content'=>'required|string'
        ]);

        $notice=\App\Notice::create(request(['title','content']));
        dispatch(new \App\Jobs\SendMessage($notice));
        return redirect("/admin/notice");
    }
    /*
     * 删除
     */
    public function delete($id)
    {
        notice::where('id', '=', $id)->delete();
        return back();
    }
    //修改界面
    public function edit(Notice $notice){
        return view('admin.notice.update',compact('notice'));
    }
    
    //修改逻辑
    public function update(Notice $notice){
        $this->validate(request(),[
            'title'=>'required|string',
            'content'=>'required|string'
        ]);
        $notice->title=$_REQUEST['title'];
        $notice->content=$_REQUEST['content'];
        $notice->save();
        return redirect("/admin/notice");
    }
    public function notice_list(){

       $user= \Auth()->user();
       $notices=$user->notices;
       return view('/admin/notice/notice_list',compact('notices'));
    }
    //批量删除逻辑
    //原理则是前端发送批量删除的ID组成的数组 通过后端逻辑进行处理
}